/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.*;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.*;

/**
 *
 * @author Asus
 */
public class Utilities {
    public static final byte TYPE_FILE = 0;
    public static final byte TYPE_FOLDER = 1;
    public static final byte TYPE_DELETED = 2;
    public static final int SERVER_PORT     = 1234;
    public static final String SERVER_HOST  = "127.0.0.1";
    public static final int SERVER_TIMEOUT  = 300000;
    public static final String SERVER_REPO  = "repositories";
    public static final long SYNC_PERIOD    = 10000;
    private static long filesize;
    private static int blocksize;
    private static long counter;
    private static byte[] block;
    
    
    public Utilities(){
        filesize = 0;
        blocksize = 0;
        counter = 0;
    }
    
    public static byte[] getFileByte(String filepath) throws FileNotFoundException{
        File file = new File(filepath);
        byte[] result = null;
        int i = 0;
        String temp = "";
        if(file.isDirectory())
            result = new byte [0];
        BufferedInputStream buff = new BufferedInputStream(new FileInputStream(filepath));
        StringBuilder sb = new StringBuilder();
        try {
            while((i = buff.read()) != -1){
                sb.append((char)i);
            }
        } catch (IOException ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        temp = sb.toString();
        result = new byte[temp.length()];
        for(int j=0;j<result.length;j++){
            result[j] = (byte) temp.charAt(j);
        }
        return result;
    }
    
    public static long getFileSize(){
        return filesize;
    }
    
    public static void setFileSize(byte[] file){
        filesize = file.length;
    }
    
    public static void setBlockSize(byte[] file){
        if(filesize < 4096){
            blocksize = file.length;
        }else{
            blocksize = 4096;
        }
        block = new byte[blocksize];
        counter = counter + blocksize;
        filesize = filesize - blocksize;
    }
    
    public static byte[] separateByte(byte[] file)
    {        
        System.arraycopy(file, (byte)counter, block, 0, blocksize);
        return block;
    }
    
    public void SendFile(String filepath, OutputStream socketOutputStream){
        try {
//                byte[] file = getFileByte(filepath);
//                setFileSize(file);
//                while(getFileSize() > 0){
//                    setBlockSize(file);
//                    byte[] sendfile = separateByte(file);
//                }
                CheckedOutputStream cs = new CheckedOutputStream(socketOutputStream, new Adler32());
                ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(cs));
                ZipEntry entry = new ZipEntry(filepath);
                zos.putNextEntry(entry);
        } catch (Exception ex) {
            Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static String convertToSHA1 (String input) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        return convertToSHA1(input.getBytes("iso-8859-1"));
    }
    
    public static String convertToSHA1 (byte[] input) throws NoSuchAlgorithmException{
        String res;
        
        MessageDigest msgdig = MessageDigest.getInstance("SHA-1");
        msgdig.update(input);
        byte[] sha1 = msgdig.digest();
        
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<sha1.length;i++){
            sb.append(Integer.toString((sha1[i] & 0xff) + 0x100, 16).substring(1));
        }
        res = sb.toString();
        return res;
    }
}
